package com.dede.getmusic.extends

import com.dede.getmusic.data.BaseResult
import com.lzy.okgo.convert.StringConvert
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.GetRequest
import com.lzy.okgo.request.PostRequest
import com.lzy.okgo.request.base.Request
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author hsh
 * @date 2017/9/30 13:50.
 * @doc
 */
fun Request<String, PostRequest<String>>.post2Rx(): Observable<Response<String>> {
    return this.converter(StringConvert())
            .adapt(ObservableResponse<String>())
            .rxScheduler()
}

fun Request<String, PostRequest<String>>.post2Rx_Base(): Observable<BaseResult> {
    return post2Rx().map { BaseResult(it.body()) }
}

fun Request<String, GetRequest<String>>.get2Rx(): Observable<Response<String>> {
    return this.converter(StringConvert())
            .adapt(ObservableResponse<String>())
            .rxScheduler()
}

fun <T> Observable<T>.rxScheduler(): Observable<T> = this.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
